<?php
namespace app\adminapiv3\service;

use app\common\model\Ip;
use app\common\service\Base as AppBase;
use think\facade\Request;

class Base extends AppBase 
{
    public $default_limit = 30;


    /**
     * @name 获取当前登录用户的信息
     * @Author wx
     * @date 2019/8/16 14:16
     */
    function getUserInfo($key = '')
    {
        $userInfo = Request::param('user_info');

        if($userInfo){

            $return = array(
                'user_id' => $userInfo['id'],
                'account' => $userInfo['real_name'],
                'user_group_id' => $userInfo['user_group_id'],
                'user_group_level' => $userInfo['user_group_level'],
            );

            if(!empty($key)){
                return $return[$key];
            }

            return $return;
            
        }else{

            returnErrJson($this->CODE_FAIL, '身份验证失败');
        }
    }


    /**
     * @name 查询当前用户可以操作的主机列表
     * @author wx
     * @date 2020/7/23 13:19
     */
    function findGroupHost()
    {
        $user = $this->getUserInfo();

        return db('host')->where('group_id', $user['user_group_id'])->column('id');
    }


    /**
     * @name 验证操作主机
     * @author wx
     * @date 2020/7/20 13:16
     */
    function checkOptionHost($host_id)
    {
        $user = $this->getUserInfo();

        $hostArr = explode(',', $host_id);


        $groups = db('host')->whereIn('id', $hostArr)->column('group_id');
        $groups = array_unique($groups);

        if (!in_array($user['user_group_id'], $groups)) {
            returnErrJson($this->CODE_FAIL,'您无法对当前主机执行此操作');
        }


    }

}